<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    *{
      margin: 0;
      padding: 0;
    }
    #box{
      width: 300px;
      height: 300px;
      background-color: red;
      transition: all .5s;
    }
  </style>
</head>
<body>

  <div id="box"></div>  
  <script>
    //promise是异步操作的一种解决方案
    document.onclick=()=>{
      console.log('这里是异步的');
    }
    console.log('这里是同步的');
    //promise一般用来解决层层嵌套的回调函数（回调地狱）
    const box=document.getElementById('box')
    const move=(el,{x=0,y=0}={},end=()=>{})=>{
      el.style.transform=`translate(${x}px,${y}px)`;
      el.addEventListener('transitionend',()=>{
        end();
      },false)
    }
    const movePromise=(el,point)=>{
      return new Promise(reslove=>{
        move(el,point,()=>{
          reslove()
        })
      })
    }
    box.onclick=function(){
      movePromise(box,{x:150}).then(()=>{
        return movePromise(box,{x:150,y:150});
      }).then(()=>{
        return movePromise(box,{y:150});
      }).then(()=>{
        return movePromise(box,{x:150});
      })
    }
    // box.onclick=function(){
    //   move(box,{x:150},()=>{
    //     move(box,{x:150,y:150},()=>{
    //       move(box,{y:150},()=>{
    //         move(box,{x:0,y:0});
    //       })
    //     })
    //   })
    // }
  </script>
</body>
</html>